//
//  ViewController.swift
//  classWork4_5repeat
//
//  Created by Qingke on 16/4/6.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var saveButton_navigationItem: UIBarButtonItem!
    @IBOutlet weak var beautyNameTextfilter: UITextField!
    @IBOutlet weak var beautyPhotoImageView: UIImageView!
    @IBOutlet weak var starNumView: Rating!
    var imgOfTheView : cellClass?  //这里没有initlized 表示没有初始化，加一个问号可以解决
    
    override func viewDidLoad() {
        super.viewDidLoad()
        saveButton_navigationItem.enabled = false
        beautyNameTextfilter.delegate = self
        if let select_content = imgOfTheView {  //这行是用来在有传入参数时候显示出来的的
            beautyNameTextfilter.text = select_content.name
            beautyPhotoImageView.image = select_content.photo
            starNumView.rating = select_content.starNum
            /* sterNumView.starNum = select_content.starNum  错误是因为没有分配好对象 ，starNum是表示在 控件里 星星的数量 ，防止这样的错误做好在这些不希望被修改的属性前加 private */
           
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func selectPhotoGesture(sender: UITapGestureRecognizer) {
        beautyNameTextfilter.resignFirstResponder()
        let pickImage = UIImagePickerController()
        pickImage.sourceType = .PhotoLibrary
        pickImage.delegate = self
        presentViewController(pickImage, animated: true, completion: nil)
        
        
    }
    
    @IBAction func cancelButtonAction(sender: UIBarButtonItem) {
        
        dismissViewControllerAnimated(true, completion: nil)
        print("dissmiss")
        navigationController?.popToRootViewControllerAnimated(true)
        print("navigetiong miss")
        /*这里两个消失方法一起使用是一种偷懒浪费效率的方法因为每个 方法都执行了一次。最好用的 presentingViewController 判断 显示方法是 什么然后依次使用不同的方法消失 */
        
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let name = beautyNameTextfilter.text ?? ""
        let photo = beautyPhotoImageView.image
        let starNum = starNumView.rating
        
        imgOfTheView = cellClass(name: name, photo: photo!, starNum: starNum)
    }
    //功能：输入框的内容为空，不能够按button
    func checkSave() {
        if let imgname = beautyNameTextfilter.text {
            saveButton_navigationItem.enabled = (!imgname.isEmpty)
        }
    }
    
}

extension ViewController : UITextFieldDelegate {
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        beautyNameTextfilter.resignFirstResponder()
        checkSave()
        return true
    }
}

extension ViewController : UIImagePickerControllerDelegate{
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        beautyPhotoImageView.image = image
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
}
extension ViewController:  UINavigationControllerDelegate{
    
}
